package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.Appmanager;
import com.ruoyi.system.domain.DrllInfo;
import com.ruoyi.system.domain.Drtypemode;
import com.ruoyi.system.service.IAppmanagerService;
import com.ruoyi.system.service.IDrllInfoService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DlldrInfo;
import com.ruoyi.system.service.IDlldrInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 项目网关配置Controller
 * 
 * @author yaozh
 * @date 2024-02-22
 */
@RestController
@RequestMapping("/system/dlldrinfo")
public class DlldrInfoController extends BaseController
{
    @Resource
    private IDlldrInfoService dlldrInfoService;

    @Autowired
    private IDrllInfoService drllInfoService;

    @Resource
    private ISysDictTypeService dictTypeService;
    @Autowired
    private IAppmanagerService appManagerService;

    /**
     * 查询项目网关配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:dlldrinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(DlldrInfo dlldrInfo,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<DlldrInfo> list = dlldrInfoService.selectDlldrInfoList(dlldrInfo,dbName);
        return getDataTable(list);
    }

    /**
     * 导出项目网关配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:dlldrinfo:export')")
    @Log(title = "项目网关配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DlldrInfo dlldrInfo,@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        List<DlldrInfo> list = new ArrayList<>();
        if(appList != null && appList.size() > 0){
            list = dlldrInfoService.selectDlldrInfoList(dlldrInfo,dbName);
        }
        if(list != null && list.size() > 0){
            List<SysDictData> data = dictTypeService.selectDictDataByType("sys_parity");//校验位的字典标识
            List<SysDictData> typeData = dictTypeService.selectDictDataByType("sys_dll_type");//驱动类型的字典标识
            Map<String, List<SysDictData>> dictDataMap = new HashMap<>();
            Map<String, List<SysDictData>> dictTypeDataMap = new HashMap<>();
            if(data != null && data.size() > 0){
                dictDataMap = data.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
            }
            if(typeData != null && typeData.size() > 0){
                dictTypeDataMap = typeData.stream().collect(Collectors.groupingBy(SysDictData::getDictValue));
            }
            for(DlldrInfo tempDllDrInfo : list){
                String parity = tempDllDrInfo.getParity();
                Long type = tempDllDrInfo.getType();
                List<SysDictData> tempList = dictDataMap.get(parity);
                List<SysDictData> tempTypeList = dictTypeDataMap.get(String.valueOf(type));
                if(tempList != null && tempList.size() > 0){
                    SysDictData sysDictData = tempList.get(0);
                    tempDllDrInfo.setParity(sysDictData.getDictLabel());
                }
                if(tempTypeList != null && tempTypeList.size() > 0){
                    SysDictData sysDictData = tempTypeList.get(0);
                    tempDllDrInfo.setTypeName(sysDictData.getDictLabel());
                }
            }
        }
        ExcelUtil<DlldrInfo> util = new ExcelUtil<DlldrInfo>(DlldrInfo.class);
        util.exportExcel(response, list, "项目网关配置数据");
    }

    @Log(title = "项目网关配置", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:dlldrinfo:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, @RequestParam String dbName) throws Exception
    {
        ExcelUtil<DlldrInfo> util = new ExcelUtil<DlldrInfo>(DlldrInfo.class);
        List<DlldrInfo> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = dlldrInfoService.importDlldrInfo(userList, updateSupport, operName, dbName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<DlldrInfo> util = new ExcelUtil<DlldrInfo>(DlldrInfo.class);
        util.importTemplateExcel(response, "项目网关配置数据");
    }

    /**
     * 获取项目网关配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:dlldrinfo:query')")
    @GetMapping(value = "/{drid}")
    public AjaxResult getInfo(@PathVariable("drid") Long drid,@RequestParam String dbName)
    {
        return success(dlldrInfoService.selectDlldrInfoByDrid(drid,dbName));
    }

    /**
     * 新增项目网关配置
     */
    @PreAuthorize("@ss.hasPermi('system:dlldrinfo:add')")
    @Log(title = "项目网关配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DlldrInfo dlldrInfo,@RequestParam String dbName)
    {
        return toAjax(dlldrInfoService.insertDlldrInfo(dlldrInfo,dbName));
    }

    /**
     * 修改项目网关配置
     */
    @PreAuthorize("@ss.hasPermi('system:dlldrinfo:edit')")
    @Log(title = "项目网关配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DlldrInfo dlldrInfo,@RequestParam String dbName)
    {
        return toAjax(dlldrInfoService.updateDlldrInfo(dlldrInfo,dbName));
    }

    /**
     * 删除项目网关配置
     */
    @PreAuthorize("@ss.hasPermi('system:dlldrinfo:remove')")
    @Log(title = "项目网关配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{drids}")
    public AjaxResult remove(@PathVariable Long[] drids,@RequestParam String dbName)
    {
        return toAjax(dlldrInfoService.deleteDlldrInfoByDrids(drids,dbName));
    }

    /**
     * 不分页，查询驱动列表
     */
    @PreAuthorize("@ss.hasPermi('system:dlldrinfo:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        List<DrllInfo> list = drllInfoService.selectDrllInfoForAll(dbName);
        return success(list);
    }
}
